package com.lang.service;

import java.util.Iterator;
import java.util.List;

import com.lang.beans.Parcel;
import com.lang.beans.User;
import com.lang.daoandimpl.ParcelDAO;
import com.lang.daoandimpl.UserDAO;

public class UserService {
     private UserDAO userdao;
     private ParcelDAO parceldao;

	public UserDAO getUserdao() {
		return userdao;
	}

	public void setUserdao(UserDAO userdao) {
		this.userdao = userdao;
	}

	public ParcelDAO getParceldao() {
		return parceldao;
	}

	public void setParceldao(ParcelDAO parceldao) {
		this.parceldao = parceldao;
	}

	/**
     * 普通用户登录函数
     * @param id
     * @param passWord
     * @return
     */
    public User loginservice(String username,String passWord){
    	//int i=Integer.valueOf(id);    //String类型转化
		List ulist = userdao.findByName(username);  //先通过id查找用户
		if(ulist.size()!=0){
			User u=(User)ulist.get(0);
			if(u.getPwd().equals(passWord)){   //比较密码
				return u;
			}
		}
		return null;
    }
    /**
     * 获取单个用户个人信息
     * @param id
     * @return
     */
    public User getUserInfo(String id){
    	int i=Integer.valueOf(id);    //String类型转化
		User u = userdao.findById(i);  //先通过id查找用户
		return u;
    }
    /**
     * 获取用户包裹
     * @param id
     * @return
     */
    public List getUserParcel(String id){
    	System.out.println("new Integer(id) = " + new Integer(id));
     	List userparcels = parceldao.findByUserId(new Integer(id));
     	Iterator itr = userparcels.iterator();
//     	while (itr.hasNext()) {
//     	    Parcel nextObj = (Parcel) itr.next();
//     	    System.out.println();
//     	}
    	return userparcels;
    }
    
    /**
     * 修改用户基本信息
     * @return
     */
    public void modifyUserInfo(User user){
    	userdao.attachDirty(user);
    }
    
    /**
     * 注册新用户
     * @return
     */
    public void register(User user){
    	userdao.save(user);
    }
}
